home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 761 < prev    next >
Encoding:
Text File  |  1996-08-06  |  4.0 KB  |  124 lines

  1. Path: chronicle.mti.sgi.com!austern
  2. From: Nico Josuttis <nico@bredex.de>
  3. Newsgroups: comp.std.c++
  4. Subject: FOR comp.std.c++: problems with I/O exceptions
  5. Date: 18 Mar 1996 09:50:37 PST
  6. Organization: -
  7. Approved: austern@isolde.mti.sgi.com
  8. Message-ID: <199603181627.RAA08990@bredex.bredex.de>
  9. NNTP-Posting-Host: isolde.mti.sgi.com
  10. X-Original-Date: Mon, 18 Mar 96 17:27:22 +0100
  11. X-Authentication-Warning: bredex.bredex.de: Host localhost didn't use HELO protocol
  12. X-Mts: smtp
  13. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  14.     iQBVAwUBMU2iiUy4NqrwXLNJAQFt1wIAvwIqt1x9jYYpmFoq6cxMxbkcbLO5QAIG
  15.     8SZMAeoBu0T6YVJq796W4rokhNWTD8wFQssNvFpQL41J1a8HW5zQtw==
  16.     =n7gZ
  17. Originator: austern@isolde.mti.sgi.com
  18.  
  19. Hi,
  20. Trying the new feature of throwing stream exceptions
  21. i wrote the following program:
  22.  
  23. ------------------------ snip ----------------------
  24. #include <iostream>
  25. #include <cstdlib>
  26. using namespace std;
  27.  
  28.  
  29. /* process and print sum of integer read from cin
  30.  */
  31. int main()
  32. {
  33.    
  34.     cin.exceptions (ios::failbit | ios::badbit);
  35.  
  36.     try {
  37.         int value, sum;
  38.  
  39.         /* while not EOF
  40.          * read and add value
  41.          */
  42.         sum = 0;
  43. #ifdef VERSION1
  44.         while (cin >> ws) {
  45.             cin >> value;
  46. #else
  47.         while (cin >> value) {
  48. #endif
  49.             sum += value;
  50.         }
  51.  
  52.         cout << "sum: " << sum << endl;
  53.  
  54.     }
  55.     catch (ios::failure error) {
  56.         cerr << "Error: " << error.what() << endl;
  57.         exit (EXIT_FAILURE);
  58.     }
  59. }
  60. ------------------------ snip ----------------------
  61.  
  62. But it didn't work as I expected, because also on EOF
  63. I ALWAYS got the failbit exception!
  64. And that happened in both versions.
  65. After some investigation I found the problem and was surprised,
  66. because the compiler and library I used had no bug.
  67. Its a feature, which we should change IMHO.
  68.  
  69. The problem is, that istream::ipfx() always calls setstate(failbit),
  70. if after any preparation good() is false.
  71. So even if only the eofbit is set (due to the skip of whitespace),
  72. the failbit is also set.
  73. I think, therefore we must change the specification of ixpf()
  74. to call setstate(eofbit) if eof() is true and setstate(failbit)
  75. if any other problem occurs.
  76.  
  77. But even if that would be fixed, i would have problems implementing
  78. a simple filter like:
  79.     char c;
  80.     while (cin.get(c)) {
  81.     cout.put(c);
  82.     }
  83. With the actual specification i see no chance to get no exception on
  84. EOF but an exception for any failure.
  85. Therefore perhaps a deeper change in the specification would be
  86. necessary, namely
  87.  - either don't set failbit if eofbit is set
  88.  - or set eofbit on eof and set the failbit with the NEXT try to read data
  89. In general I need a chance to handle eof without getting
  90. failbit exceptions.
  91.  
  92. ONE OTHER POINT I found:
  93. You might say, that i could catch all exceptions and look at the
  94. bits.
  95. BUT i got no information about the reason for I/O exceptions.
  96. I have only what() to get a
  97. undefined message, but I think informations about the stream
  98. or at least about the state of the stream would be senseful
  99. to process exceptions on EOF different from other problems.
  100. So for ios_base::failure an additional member like
  101. state, which is rdstate() of the stream would be very senseful.
  102.  
  103. For example I'd like to open file and process its data until EOF
  104. without any exception, but having exceptions for any error
  105. and the information to handle it properly.
  106. If for example i open 2 files i have no chance to check
  107. which file got the problem.
  108.  
  109. Please correct me if I'am wrong or don't see any simple solution.
  110. --------
  111. Nico                             address: BREDEX GmbH
  112. email:   nico@bredex.de                   Nicolai Josuttis
  113.                                           Fallersleber-Tor-Wall 23
  114. phone:   +49 531 24330-0                  D-38100 Braunschweig
  115. fax:     +49 531 24330-99                 Germany
  116. --------
  117. ---
  118. [ comp.std.c++ is moderated.  To submit articles: Try just posting with your 
  119.                 newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  120.   comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  121.   Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  122.   Comments? mailto:std-c++-request@ncar.ucar.edu 
  123. ]
  124.